﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Threading;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace Client
{
    [Flags]
    public enum MsgBoxBtnType
    {
        Yes = 1,
        No = 2,
        Ok = 4,
        Cancel = 8,
        EditCtrl = 256
    }

    public class MirMsgBox : Window
    {
        public Button DMsgDlgOk;
        public Button DMsgDlgYes;
        public Button DMsgDlgNo;
        public Button DMsgDlgCancel;

        private int msglx, msgly;

        public string MsgText;
        public int DialogSize;
        public int ClickTime;

        public MirMsgBox(WinCtrl parentCtrl)
            : base(parentCtrl)
        {
            DrawColor = Color.FromArgb(150, DrawColor);

            MsgText = string.Empty;
            DialogSize = 1;
            ClickTime = 0;

            Texture d = ImageLib.Interface[360];
            if (d != null)
            {
                SetImgIndex(ImageLib.Interface, 360);
                Left = (ClientGlobals.ScreenWidth - d.GetLevelDescription(0).Width) / 2;
                Top = (ClientGlobals.ScreenHeight - d.GetLevelDescription(0).Height) / 2;
                OnDirectPaint += new OnDirectPaintHandler(MirMsgBox_OnDirectPaint);
            }


            DMsgDlgOk = new Button(this);
            DMsgDlgOk.SetImgIndex(ImageLib.Interface, 350);
            DMsgDlgOk.Top = 126;
            DMsgDlgOk.OnDirectPaint += new OnDirectPaintHandler(DMsgDlgOk_OnDirectPaint);
            DMsgDlgOk.OnClick += new OnClickExHandler(DMsgDlgOk_OnClick);

            DMsgDlgYes = new Button(this);
            DMsgDlgYes.SetImgIndex(ImageLib.Interface, 352);
            DMsgDlgYes.Top = 126;
            DMsgDlgYes.OnDirectPaint += new OnDirectPaintHandler(DMsgDlgOk_OnDirectPaint);
            DMsgDlgYes.OnClick += new OnClickExHandler(DMsgDlgOk_OnClick);

            DMsgDlgNo = new Button(this);
            DMsgDlgNo.SetImgIndex(ImageLib.Interface, 356);
            DMsgDlgNo.Top = 126;
            DMsgDlgNo.OnDirectPaint += new OnDirectPaintHandler(DMsgDlgOk_OnDirectPaint);
            DMsgDlgNo.OnClick += new OnClickExHandler(DMsgDlgOk_OnClick);

            DMsgDlgCancel = new Button(this);
            DMsgDlgCancel.SetImgIndex(ImageLib.Interface, 354);
            DMsgDlgCancel.Top = 126;
            DMsgDlgCancel.OnDirectPaint += new OnDirectPaintHandler(DMsgDlgOk_OnDirectPaint);
            DMsgDlgCancel.OnClick += new OnClickExHandler(DMsgDlgOk_OnClick);
        }

        void MirMsgBox_OnDirectPaint(object sender, Sprite spriteBatch)
        {
            Window w = sender as Window;
            Texture d = WLib[w.FaceIndex];
            if (d != null)
                spriteBatch.Draw(d, Rectangle.Empty, Vector3.Empty, new Vector3(w.SurfaceX(w.Left), w.SurfaceY(w.Top), 0), DrawColor);

            int ly = msgly;
            string[] lines = MsgText.Split(new char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries);

            if (lines.Length > 0)
            {
                for (int i = 0; i < lines.Length; i++)
                {
                    string data = lines[i];

                    if (data != String.Empty)
                        DirectText.Me.DrawText(data, w.SurfaceX(w.Left + msglx), w.SurfaceY(w.Top + ly), Color.White);
                    ly += 14;
                }
            }

            /*if ViewDlgEdit then begin
               if not EdDlgEdit.Visible then begin
                  EdDlgEdit.Visible := TRUE;
                  EdDlgEdit.SetFocus;
               end;
            end;*/
        }

        void DMsgDlgOk_OnClick(object sender, Point ptMouse)
        {
            if (sender == DMsgDlgOk) DialogResult = DialogResult.Ok;
            if (sender == DMsgDlgYes) DialogResult = DialogResult.Yes;
            if (sender == DMsgDlgNo) DialogResult = DialogResult.No;
            if (sender == DMsgDlgCancel) DialogResult = DialogResult.Cancel;

            /*if GameClose then begin
               FrmMain.Close;
            end;

            BoMsgDlgTimeCheck := False;*/
            ClickTime = Environment.TickCount;
            Visible = false;
        }

        void DMsgDlgOk_OnDirectPaint(object sender, Sprite spriteBatch)
        {
            Button b = sender as Button;
            Texture d;
            if (!b.Downed) d = WLib[b.FaceIndex]; else d = WLib[b.FaceIndex + 1];
            if (d != null)
                spriteBatch.Draw(d, Rectangle.Empty, Vector3.Empty, new Vector3(b.SurfaceX(b.Left), b.SurfaceY(b.Top), 0), DrawColor);
        }

        public void ShowMsgBox(string msgstr, MsgBoxBtnType DlgButtons)
        {
            const int XBase = 329;

            int lx, ly;
            Texture d;

            lx = XBase;
            ly = 126;
            switch (DialogSize)
            {
                case 0:  //ÀÛÀº°Å
                    {
                        d = ImageLib.Interface[381];
                        if (d != null)
                        {
                            SetImgIndex(ImageLib.Interface, 381);
                            Left = (ClientGlobals.ScreenWidth - d.GetLevelDescription(0).Width) / 2;
                            Top = (ClientGlobals.ScreenHeight - d.GetLevelDescription(0).Height) / 2;
                            msglx = 39;
                            msgly = 38;
                            lx = 90; //d.Width div 2 - 38; //XBase;
                            ly = 36; //56;
                        }
                        break;
                    }
                case 1:  //³Ð°í Å«°Å
                    {
                        d = ImageLib.Interface[360];
                        if (d != null)
                        {
                            SetImgIndex(ImageLib.Interface, 360);
                            Left = (ClientGlobals.ScreenWidth - d.GetLevelDescription(0).Width) / 2;
                            Top = (ClientGlobals.ScreenHeight - d.GetLevelDescription(0).Height) / 2;
                            msglx = 30;
                            msgly = 30;
                            lx = XBase;
                            ly = 143;
                        }
                        break;
                    }
                case 2:  //±æÀº°Å
                    {
                        d = ImageLib.Interface[380];
                        if (d != null)
                        {
                            SetImgIndex(ImageLib.Interface, 380);
                            Left = (ClientGlobals.ScreenWidth - d.GetLevelDescription(0).Width) / 2;
                            Top = (ClientGlobals.ScreenHeight - d.GetLevelDescription(0).Height) / 2;
                            msglx = 40;
                            msgly = 50;
                            lx = 137;
                            ly = 413;
                        }
                        break;
                    }
            }

            MsgText = msgstr;
            //ViewDlgEdit = false;
            Floating = true;   //¸Þ¼¼Áö ¹Ú½º°¡ ¶°´Ù´Ô..
            DMsgDlgOk.Visible = false;
            DMsgDlgYes.Visible = false;
            DMsgDlgCancel.Visible = false;
            DMsgDlgNo.Visible = false;
            Left = (ClientGlobals.ScreenWidth - Width) / 2;
            Top = (ClientGlobals.ScreenHeight - Height) / 2;


            if ((DlgButtons & MsgBoxBtnType.Cancel) > 0)
            {
                DMsgDlgCancel.Left = lx;
                DMsgDlgCancel.Top = ly;
                DMsgDlgCancel.Visible = true;
                lx -= 110;
            }
            if ((DlgButtons & MsgBoxBtnType.No) > 0)
            {
                DMsgDlgNo.Left = lx;
                DMsgDlgNo.Top = ly;
                DMsgDlgNo.Visible = true;
                lx -= 110;
            }
            if ((DlgButtons & MsgBoxBtnType.Yes) > 0)
            {
                DMsgDlgYes.Left = lx;
                DMsgDlgYes.Top = ly;
                DMsgDlgYes.Visible = true;
                lx -= 110;
            }
            if ((DlgButtons & MsgBoxBtnType.Ok) > 0 || lx == XBase)
            {
                DMsgDlgOk.Left = lx;
                DMsgDlgOk.Top = ly;
                DMsgDlgOk.Visible = true;
                lx -= 110;
            }

            WinManager.Me.HideAllControls();
            ShowModal();

            if ((DlgButtons & MsgBoxBtnType.EditCtrl) > 0)
            {
                //ViewDlgEdit := TRUE; //¿¡µðÆ® ÄÁÆ®·ÑÀÌ º¸¿©¾ß ÇÏ´Â °æ¿ì.
                Floating = false;

                /*EdDlgEdit.Text = '';
                EdDlgEdit.Width = DMsgDlg.Width - 70;
                EdDlgEdit.Left = (Global.ScreenWidth - EdDlgEdit.Width) / 2;
                EdDlgEdit.Top  = (Global.ScreenHeight - EdDlgEdit.Height) / 2 - 10;
                EdDlgEdit.MaxLength := MsgDlgMaxStr;*/
            }


            //EdDlgEdit.Visible := FALSE;
            WinManager.Me.RestoreHideControls();
            //DlgEditText = EdDlgEdit.Text;
            //if (PlayScene.EdChat.Visible) PlayScene.EdChat.SetFocus;
            //ViewDlgEdit = false;
            DialogSize = 1; //±âº»»óÅÂ
            //RunDice = 0;
            //BoDrawDice = false;
        }
    }
}